// 消息类型的测试
#include "message.hpp"

int main()
{
    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::REQ_RPC);
    // rcrpc::RpcRequest::ptr  rrp = std::dynamic_pointer_cast<rcrpc::RpcRequest>(bmp);
    
    // rcrpc::RpcRequest::ptr rrp = rcrpc::MessageFactory::create<rcrpc::RpcRequest>();
    // Json::Value param;
    // param["num1"] = 11;
    // param["num2"] = 22;
    // rrp->setMethod("add");
    // rrp->setParams(param);
    // std::string str = rrp->serialize();
    // std::cout << str << std::endl;
    // rrp->check();

    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::REQ_RPC);
    // bool ret = bmp->unserialize(str);
    // std::cout << "ret:" << ret << std::endl;
    // rcrpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<rcrpc::RpcRequest>(bmp);
    // std::cout << rrp2->method() << std::endl;
    // std::cout << rrp2->params()["num1"].asInt() <<std::endl;
    // std::cout << rrp2->params()["num2"].asInt() <<std::endl;

    // rcrpc::TopicRequest::ptr trp = rcrpc::MessageFactory::create<rcrpc::TopicRequest>();
    // trp->setTopicKey("news");
    // trp->setOptype(rcrpc::TopicOptype::TOPIC_PUBLISH);
    // trp->setTopicMsg("hello world!");
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;
    // trp->check();

    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::REQ_TOPIC);
    // bool ret = bmp->unserialize(str);
    // std::cout << "ret:" << ret << std::endl;
    // rcrpc::TopicRequest::ptr trp2 = std::dynamic_pointer_cast<rcrpc::TopicRequest>(bmp);
    // std::cout << trp2->topicKey() << std::endl;
    // std::cout << trp2->topicMsg() << std::endl;
    // std::cout << (int)trp2->optype() << std::endl;

    // rcrpc::ServiceRequest::ptr srp = rcrpc::MessageFactory::create<rcrpc::ServiceRequest>();
    // srp->setMethod("Add");
    // srp->setOptype(rcrpc::ServiceOptype::SERVICE_DISCOVER);
    // srp->setHost(rcrpc::Address("127.0.0.1",9090));
    // std::string str = srp->serialize();
    // std::cout << str << std::endl;
    // bool ret = srp->check();
    // if( ret == false){
    //     std::cout << "check failed" << std::endl;
    //     return -1;
    // }

    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::REQ_SERVICE);
    // ret = bmp->unserialize(str);
    // if( ret == false){
    //     std::cout << "unserialize ailed" << std::endl;
    //     return -1;
    // }
    // rcrpc::ServiceRequest::ptr srp2 = std::dynamic_pointer_cast<rcrpc::ServiceRequest>(bmp);
    // std::cout << (int)srp2->optype() << std::endl;
    // std::cout << srp2->method() <<std::endl;
    // std::cout << srp2->host().first <<std::endl;
    // std::cout << srp2->host().second <<std::endl;


    // rcrpc::RpcResponse::ptr rrp = rcrpc::MessageFactory::create<rcrpc::RpcResponse>();
    // rrp->setRCode(rcrpc::RCode::RECODE_OK);
    // rrp->setResult(33);
    // std::string str = rrp->serialize();
    // std::cout << str << std::endl;
    // rrp->check();

    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::RSP_RPC);
    // bool ret = bmp->unserialize(str);
    // std::cout << "ret:" << ret << std::endl;
    // rcrpc::RpcResponse::ptr rrp2 = std::dynamic_pointer_cast<rcrpc::RpcResponse>(bmp);
    // std::cout << (int)rrp2->rcode() <<std::endl;
    // std::cout << rrp2->result().asInt() <<std::endl;

    // rcrpc::TopicResponse::ptr trp = rcrpc::MessageFactory::create<rcrpc::TopicResponse>();
    // trp->setRCode(rcrpc::RCode::RECODE_OK);
    // std::string str = trp->serialize();
    // std::cout << str << std::endl;
    // trp->check();

    // rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::RSP_TOPIC);
    // bool ret = bmp->unserialize(str);
    // std::cout << "ret:" << ret << std::endl;
    // rcrpc::TopicResponse::ptr trp2 = std::dynamic_pointer_cast<rcrpc::TopicResponse>(bmp);
    // std::cout << (int)trp2->rcode() <<std::endl;

    rcrpc::ServiceResponse::ptr srp = rcrpc::MessageFactory::create<rcrpc::ServiceResponse>();
    srp->setRCode(rcrpc::RCode::RECODE_OK);
    srp->setMethod("Add");
    srp->setOptype(rcrpc::ServiceOptype::SERVICE_DISCOVERY);
    std::vector<rcrpc::Address> addrs;
    addrs.push_back(rcrpc::Address("127.0.0.1",9090));
    addrs.push_back(rcrpc::Address("127.0.0.2",9091));
    srp->setHost(addrs);
    std::string str = srp->serialize();
    std::cout << str << std::endl;
    bool ret = srp->check();
    if( ret == false){
        std::cout << "check failed" << std::endl;
        return -1;
    }

    rcrpc::BaseMessage::ptr bmp = rcrpc::MessageFactory::create(rcrpc::MType::RSP_SERVICE);
    ret = bmp->unserialize(str);
    if( ret == false){
        std::cout << "unserialize ailed" << std::endl;
        return -1;
    }
    rcrpc::ServiceResponse::ptr srp2 = std::dynamic_pointer_cast<rcrpc::ServiceResponse>(bmp);
    std::cout << (int)srp2->rcode() <<std::endl;
    std::cout << (int)srp2->optype() << std::endl;
    std::cout << srp2->method() <<std::endl;
    std::vector<rcrpc::Address> addrs2 = srp->hosts();
    for(auto& addr :addrs){
        std::cout << addr.first << " " << addr.second << std::endl;
    }

    return 0;
}